<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Model for channel
 */
class Default_Model_Channel extends At_Model{
    const TYPE_1    = 'single';
    const TYPE_2    = 'multi';
    const TYPE_3    = 'multi-open';
    const TYPE_4    = 'single-open';
	const TYPE_5	= 'comment';

	// class values
    protected $_id;
    protected $_name;
    protected $_type;
	protected $_comment;
    protected $_description;

    //external properties
    private $keywords;
	private $movie;

	public function setMovie( Default_Model_Movie $movie ){
		$this->movie = $movie;
	}

	public function getMovie(){
		return $this->movie;
	}

    public function toArray(){
		$config = parent::toArray();

		$config['properties'] = $this->getConfig();
		$config['keywords'] = array();

		foreach( $this->getKeywords() as $keyword ){
			$config['keywords'][] = $keyword->toArray();
		}

		return $config;
    }

	public function delete(){
		$this->getMapper()->delete( $this );
	}

	/**
	 * return a list of properties
	 * @todo This should really be a model, as others depend on it too.
	 * 			This data should be configured as a database table
	 *
	 * @return array key=>value array wht this object's ocnfiguration
	 */
	public function getConfig(){
		$types = array(
            1   => self::TYPE_1,
            2   => self::TYPE_2,
            3   => self::TYPE_3,
            4   => self::TYPE_4,
            5   => self::TYPE_5,
        );

		//$types = $this->getTypes();
		$type  = $this->__get('type');

		if( isset( $types[$type] ) ){
			$name = $types[$type];
			$keys = explode('-', $name );
			foreach( $keys as $key ){
				$default[$key] = true;
			}
		}

		return (object) $default;
	}

	/**
	 * Get an array of keywords associates with this channel
	 */
    public function getKeywords(){
        if( null === $this->keywords && null !== $this->id ){
            $keyword = new Default_Model_Keyword();
			$keyword->setChannel( $this );
            $this->keywords   = $keyword->fetchAll();
        }
        return $this->keywords;
    }

	/**
	 * Set an array of keywords to be associated with this channel
	 */
    public function setKeywords( array $keywords = array() ){
		$selected = array();

		foreach( $keywords as $key => $value ){
			if( is_string($value) ){
				$keyword = new Default_Model_Keyword();
				$keyword->name = $value;
				if( null == $keyword->id ){
					$keyword->save();
				}

				$keywords[$key] = $keyword;
				$value = $keyword;
			}

			if( in_array( $value->id, $selected ) ){
				unset( $keywords[$key] );
			}

			$selected[] = $value->id;
		}

		$this->keywords = $keywords;
    }
}
